//
//  ECButton.m
//  ERPChargeAccount2
//
//  Created by 刘思麟 on 2018/6/17.
//  Copyright © 2018年 历山大亚. All rights reserved.
//

#import "ECButton.h"

@interface ECButton()

@property (nonatomic,strong)UILabel * redDot;

@end

@implementation ECButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.redDot];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.splitBetweenImgAndTitle = 0 * PX_RELATIVE375;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGSize imgSize = self.imageView.size;
    CGSize labelSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font width:CGFLOAT_MAX];
    
    switch (self.imgLocation) {
        case ECButonImageLocationLeft:
            return;
            break;
        case ECButonImageLocationTop:{
            
            self.imageView.frame = CGRectMake((self.size.width - imgSize.width) / 2, (self.size.height - imgSize.height - labelSize.height - self.splitBetweenImgAndTitle) / 2, imgSize.width, imgSize.height);
            
            self.titleLabel.frame = CGRectMake((self.size.width - labelSize.width) / 2, (self.size.height - labelSize.height - imgSize.height - self.splitBetweenImgAndTitle) / 2 + imgSize.height + self.splitBetweenImgAndTitle,/** labelSize.width > self.width ?self.width:*/labelSize.width, labelSize.height);
        }
            break;
        case ECButonImageLocationRight: {
            self.titleLabel.frame = CGRectMake((self.width - imgSize.width - labelSize.width - self.splitBetweenImgAndTitle) >=0?(self.width - imgSize.width - labelSize.width - self.splitBetweenImgAndTitle) / 2:5 * PX_RELATIVE375, (self.height - labelSize.height)/2, /**(labelSize.width + self.splitBetweenImgAndTitle + self.imageView.width) > self.width?(self.width - self.splitBetweenImgAndTitle - self.imageView.width):*/labelSize.width, labelSize.height);
            
            self.imageView.frame = CGRectMake(self.titleLabel.right + self.splitBetweenImgAndTitle , (self.height - imgSize.height) / 2, imgSize.width, imgSize.height);
            
            CGFloat inMaxW = MAX(MAX(self.titleLabel.right, self.imageView.right) - MIN(self.titleLabel.x, self.imageView.x) + 10 * PX_RELATIVE375,self.width);
            if (self.width != inMaxW) {
                CGRect inFrame = self.frame;
                inFrame.size.width = inMaxW;
                if (self.width < inMaxW) {
                    CGFloat inSplitW = fabs(MAX(self.titleLabel.right, self.imageView.right) - MIN(self.titleLabel.x, self.imageView.x) + 10 * PX_RELATIVE375 - self.width);
                    inFrame.origin.x -= inSplitW;
                }
                if (!CGRectEqualToRect(self.frame,inFrame)) {
//                    self.frame = inFrame;
                }
            }
        }
            break;
        default:
            return;
            break;
    }
    
    if (self.imgLocation != ECButonImageLocationTop) {
        return;
    }
    if (self.btState == ECButonStateunRead) {
        self.redDot.hidden = NO;
    }else {
        self.redDot.hidden = YES;
    }
    CGSize size = self.redDot.frame.size;
    self.redDot.frame = CGRectMake(CGRectGetMaxX(self.imageView.frame) - size.width/2, CGRectGetMinY(self.imageView.frame) - size.height / 2, size.width, size.height);
    CGRect frame = self.frame;
    CGFloat maxW = MAX(MAX(self.titleLabel.right, self.imageView.right) - MIN(self.titleLabel.x, self.imageView.x) + 10 * PX_RELATIVE375,self.width);
    CGFloat maxH = MAX(MAX(self.titleLabel.bottom, self.imageView.bottom) - MIN(self.titleLabel.y, self.imageView.y) + 10 * PX_RELATIVE375,self.height);
    frame.origin.x += (CGRectGetWidth(frame) - maxW)/2;
    frame.origin.y += (CGRectGetHeight(frame) - maxH)/2;
    frame.size.width = maxW;
    frame.size.height = maxH;
    if (!CGRectEqualToRect(self.frame,frame)) {
//        self.frame = frame;
    }
}

- (UILabel *)redDot {
    if (!_redDot) {
        _redDot = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        _redDot.clipsToBounds = YES;
        _redDot.hidden = YES;
        _redDot.layer.cornerRadius = 5;
        _redDot.backgroundColor = [UIColor redColor];
    }
    return _redDot;
}

@end
